package org.westboy.common.util;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.westboy.common.constant.Const;
import org.westboy.common.exception.ApplicationException;

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;

/**
 * Description:
 *
 * @author Jin
 * @create 2017-12-27
 **/
public class AppUtil {

    public static void checkRepeatRun() throws ApplicationException {
        try {
            ServerSocketChannel.open().socket().bind(new InetSocketAddress(Const.SOCKET_PORT));
        }catch (Exception e){
            throw new ApplicationException("请不要重复运行程序！");
        }
    }

    public static void showErrorAlert(Throwable throwable) {
        Platform.runLater(()->{
            Alert alert = new Alert(Alert.AlertType.ERROR, throwable.getMessage());
            alert.showAndWait().ifPresent(r-> Platform.exit());

        });
    }

    public static void showSuccessAlert(String msg) {
        Platform.runLater(()->{
            Alert alert = new Alert(Alert.AlertType.INFORMATION, msg);
            alert.showAndWait();

        });
    }

    public static void showErrorAlert(String msg) {
        Platform.runLater(()->{
            Alert alert = new Alert(Alert.AlertType.WARNING, msg);
            alert.showAndWait();

        });
    }


    public static ImageView getDocumentImageView(){
     return  new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/document_16x16.png").toExternalForm()));
    }

    public static ImageView getImageImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/image_16x16.png").toExternalForm()));
    }

    public static ImageView getVideoImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/video_16x16.png").toExternalForm()));
    }

    public static ImageView getTxtImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/txt_16x16.png").toExternalForm()));
    }

    public static ImageView getFolderImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/folder_16x16.png").toExternalForm()));
    }

    public static ImageView getOtherImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/other_16x16.png").toExternalForm()));
    }

    public static ImageView getShareImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/share_16x16.png").toExternalForm()));
    }

    public static ImageView getShareMeImageView(){
        return   new ImageView(new Image(AppUtil.class.getClass().getResource("/icons/share_me_16x16.png").toExternalForm()));
    }
}
